﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Data;

namespace SalaryChannel.Web
{
    [ServiceContract(Namespace = "http://salary.yangchao.ts.gs.chinamobile/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AdviseService: ServiceAuthRoot
    {
       
        [OperationContract]
        public List<AdviceEntity> GetAdviseByUserID(string UserID, int PageSize, int PageIndex)
        {
            using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
            {
                try
                {
                    Authoricate();
                    List<AdviceEntity> list = new List<AdviceEntity>(PageSize);
                    //r query = 1;
                    if (string.IsNullOrEmpty(UserID))
                    {
                        var query = from q in context.SQ_Advice
                                    orderby q.CreateTime descending
                                    select new
                                    {
                                        q.ID,
                                        q.Title
                                    };


                        foreach (var v in query.Skip((PageIndex - 1) * PageSize).Take(PageSize))
                        {
                            list.Add(new AdviceEntity { ID = v.ID, Title = v.Title });
                        }
                    }
                    else
                    {
                        var query = from q in context.SQ_Advice
                                    where q.CreateID == UserID
                                    orderby q.CreateTime descending
                                    select new
                                    {
                                        q.ID,
                                        q.Title
                                    };


                        foreach (var v in query.Skip((PageIndex - 1) * PageSize).Take(PageSize))
                        {
                            list.Add(new AdviceEntity { ID = v.ID, Title = v.Title });
                        }
                    }
                    return list;
                }
                catch (Exception ex)
                {
                    LogManager.Write("获取Advise_Type失败,"+ex.Message+"--" + HttpContext.Current.Request.UserHostAddress);
                    return null;
                }
            }
        }

        // 在此处添加更多操作并使用 [OperationContract] 标记它们

        [OperationContract]
        public AdivceDetail GetAdviseByID(int ID)
        {
            using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
            {
                try
                {
                    Authoricate();
                    var query= from q in context.SQ_Advice join user in context.UserInfor 
                                                            on q.CreateID equals user.User_ID
                                                            into q_user
                                                            from qu in q_user
                                                            where q.ID==ID
                                                            select new 
                                                            {
                                                                q.ID,
                                                                q.CreateID,
                                                                qu.UserName,
                                                                q.CreateTime,
                                                                q.Title,
                                                                q.Body,
                                                                q.Tel
                                                            };
                    if (query.Any())
                    {
                        var a=query.First();
                        return new AdivceDetail
                        {
                            ID = a.ID,
                            CreateID = a.CreateID,
                            CreaterName = a.UserName,
                            CreateTime = a.CreateTime,
                            Title = a.Title,
                            Body = a.Body,
                            Tel = a.Tel
                        };
                    }
                    return null;
                }
                catch (Exception ex)
                {
                    LogManager.Write("获取Advice失败," + ex.Message + 
                        "--" + HttpContext.Current.Request.UserHostAddress);
                    return null;
                }
            }
        }

        [OperationContract]
        public bool InsertAdvise(
            string CreateID,
            string Title,
            string Body,
            string Tel)
        {
            using (DataClassesQueryDataContext context = new DataClassesQueryDataContext())
            {
                try
                {
                    Authoricate();
                    context.SQ_Advice.InsertOnSubmit(new SQ_Advice{
                         CreateID = CreateID,
                         CreateTime = DateTime.Now,
                         Title = Title,
                         Body = Body,
                         Tel =Tel
                    });
                    context.SubmitChanges();
                    string EResult = string.Empty;

                    var query = from u in context.UserInfor
                                join c in context.ChannelAccount on u.Channel_ID equals c.Id
                                where u.User_ID == CreateID
                                select new { u.User_ID, u.UserName, u.Channel_ID, c.ChannelName };

                    string msg = Body;
                    if (query.Any())
                    {
                        msg = string.Format("[{0}]{1},[{2}]{3},提交建议：{4}。联系电话：{5} 。 请审阅！",
                            CreateID, query.First().UserName, query.First().Channel_ID,
                            query.First().ChannelName,
                            Body,
                            Tel);
                    }

                    SendEmail(string.Format("渠道建议[{0}]", Title), msg, EmailReceivers);
                    
                    return true;
                }
                catch (Exception ex)
                {
                    LogManager.Write("插入Advise失败," + ex.Message +
                        "--" + HttpContext.Current.Request.UserHostAddress);
                    return false;
                }
            }
        }
    }

    public class AdviceEntity
    {
        public int ID{set;get;}
        public string Title {get;set;}
    }

    public class AdivceDetail
    {
        public int ID { get; set; }
        public string CreateID { get; set; }
        public string CreaterName { get; set; }
        public DateTime CreateTime { get; set; }
        public string Title { set; get; }
        public string Body { get; set; }
        public string Tel { get; set; }
    }
        
    
}
